﻿//------------------------------------------------------------------------------
// System Name:   Customer Service Platform
// Component:     CIS  Domestic FileDown
// Created By:    Samara Jiang (samaraj@wicresoft.com)
// Created Date:  2011-2-12
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASBCC.Business.CIS;

namespace BEGDC.CC.Web.CIS2.Domestic
{
    /// <summary>
    ///  CIS  Domestic FileDown
    /// </summary>
    public partial class FileDown : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            string pathID = Request.QueryString["DirectoryID"];
            int directoryID = Convert.ToInt32(pathID);
            var paths = BranchContactsHelper.GetFilePath(directoryID);
            string path = Convert.ToString(paths);
            if (!File.Exists(path))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('文件不存在！'); history.back()", true);
                return;
            }
            new FileInfo(path).Attributes = FileAttributes.Normal;
            FileStream r = new FileStream(path, FileMode.Open);

            Response.Buffer = false;
            Response.AddHeader("Connection", "Keep-Alive");
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(path)));
            Response.AddHeader("Content-Length", r.Length.ToString());
            while (true)
            {
                byte[] buffer = new byte[1024];

                int leng = r.Read(buffer, 0, 1024);
                if (leng == 0)
                    break;
                if (leng == 1024)
                    Response.BinaryWrite(buffer);
                else
                {
                    byte[] b = new byte[leng];
                    for (int i = 0; i < leng; i++)
                        b[i] = buffer[i];
                    Response.BinaryWrite(b);
                }
            }
            r.Close();
            Response.End();

        }

    }
}
